package com.example.compose

import android.R
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import com.example.compose.ui.wifi.WIFIManager


class ForeService : Service() {

    private var  wifiStateReceiver:BroadcastReceiver? = null

    override fun onCreate() {
        super.onCreate()

    }

    override fun onBind(intent: Intent?): IBinder {

        return Binder()
    }

    override fun onRebind(intent: Intent?) {
        super.onRebind(intent)

    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val mBuilder = NotificationCompat.Builder(App().getContext()).setAutoCancel(true) // 点击后让通知将消失
        mBuilder.setContentText("测试")
        mBuilder.setContentTitle("测试")
        mBuilder.setSmallIcon(R.mipmap.sym_def_app_icon)
        mBuilder.setWhen(System.currentTimeMillis()) //通知产生的时间，会在通知信息里显示
        mBuilder.priority = Notification.PRIORITY_DEFAULT //设置该通知优先级
        mBuilder.setOngoing(false) //ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
        mBuilder.setDefaults(Notification.DEFAULT_ALL) //向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合：
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val manager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
            val channelId = "channelId" + System.currentTimeMillis()
            val channel = NotificationChannel(
                channelId,
                resources.getString(R.string.ok),
                NotificationManager.IMPORTANCE_HIGH
            )
            manager!!.createNotificationChannel(channel)
            mBuilder.setChannelId(channelId)
        }
        mBuilder.setContentIntent(null)
        startForeground(222, mBuilder.build())
        wifiReceiver()
        return super.onStartCommand(intent, flags, startId)
    }

    private fun wifiReceiver() {
        val filter = IntentFilter()
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION)
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION)
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
        filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)
        wifiStateReceiver = WifiStateReceiver()
        registerReceiver(wifiStateReceiver, filter)
    }

    inner class WifiStateReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
            Log.d("TAG","${intent.action} ->wifiState:${intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0)}")
            // 这个监听wifi的打开与关闭，与wifi的连接无关
            var wifiManager = WIFIManager(context = baseContext)
            Log.d("wifi_connectionInfo",wifiManager.connectionInfo().toString())

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(wifiStateReceiver)
        stopForeground(true) // 停止前台服务--参数：表示是否移除之前的通知
    }

    companion object {
        private const val TAG = "ForeService"
    }
}
